package com.ubst.smartad.impl;

import java.sql.*;
import java.util.*;
import java.io.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.ubst.smartad.dao.TSASystemPolicyOPTDO;
import com.ubst.smartad.helper.CryptHelper;
import com.ubst.smartad.common.TimeUtil;
import com.ubst.smartad.logger.Debug;

public class TSASystemPolicyOPTMngImpl {
	
	Connection con = null;
	private CryptHelper chelper;
	private String serverName;
	private Debug debug;

	/** 
     * 생성자
     *   
     *   
    */ 
	public TSASystemPolicyOPTMngImpl(Connection con, String serverName, String log_level) {
		this.con = con;
		this.chelper = new CryptHelper();
		this.serverName = serverName;
		this.debug = new Debug("TSASystemPolicyOPTMngImpl", log_level);

	}
	
	/** 
	 * @author KJH
     * 시스템 기본정책 등록
     *   
     */  
    public void insertSystemPolicyOPT(TSASystemPolicyOPTDO policyOPTDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

			long l_cur_date = TimeUtil.getCurrentTime();

            queryBuf = new StringBuffer();
			queryBuf.append("INSERT INTO TSA_SYSTEM_POLICYOPT " )
				.append(" ( ")
				.append("  SYSTEMID ,PASSMAXDAY ,PASSMINDAY ,PASSWARN ,INACTIVEDAY ")
				.append(" ,LOGINRETRY ,RETRYRESET ,SUSPENDTIME ,UNUSEDTIME ,SESSIONCNT ")
				.append(" ,SESSIONTIMEOUT ,PASSMINLEN ,PASSMAXLEN ,PASSHISTORY ,PASSMINALPHA ")
				.append(" ,PASSMINDIGIT ,PASSMINUALPHA ,PASSMINLALPHA ,PASSMINSPECIAL ,PASSMINDIFF ")
				.append(" ,PASSMAXSEQ ,PASSMAXREPEAT ,DEFAULTHOME ,DEFAULTSHELL ,CRESULT ")
				.append(" ,NSTATUS ,LASTSYNCDATE ")
				.append(" ) ")
				.append(" VALUES")
				.append(" ( ") 
				.append("  ? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append(" ,? ,? ")
				.append(" ) ");

			debug.debug("insertSystemPolicyOPT ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, policyOPTDO.systemid);
			pstmt.setInt(idx++, policyOPTDO.passmaxday);
			pstmt.setInt(idx++, policyOPTDO.passminday);
			pstmt.setInt(idx++, policyOPTDO.passwarn);
			pstmt.setInt(idx++, policyOPTDO.inactiveday);
			pstmt.setInt(idx++, policyOPTDO.loginretry);
			pstmt.setInt(idx++, policyOPTDO.retryreset);
			pstmt.setInt(idx++, policyOPTDO.suspendtime);
			pstmt.setInt(idx++, policyOPTDO.unusedtime);
			pstmt.setInt(idx++, policyOPTDO.sessioncnt);
			pstmt.setInt(idx++, policyOPTDO.sessiontimeout);
			pstmt.setInt(idx++, policyOPTDO.passminlen);
			pstmt.setInt(idx++, policyOPTDO.passmaxlen);
			pstmt.setInt(idx++, policyOPTDO.passhistory);
			pstmt.setInt(idx++, policyOPTDO.passminalpha);
			pstmt.setInt(idx++, policyOPTDO.passmindigit);
			pstmt.setInt(idx++, policyOPTDO.passminualpha);
			pstmt.setInt(idx++, policyOPTDO.passminlalpha);
			pstmt.setInt(idx++, policyOPTDO.passminspecial);
			pstmt.setInt(idx++, policyOPTDO.passmindiff);
			pstmt.setInt(idx++, policyOPTDO.passmaxseq);
			pstmt.setInt(idx++, policyOPTDO.passmaxrepeat);
			pstmt.setString(idx++, policyOPTDO.defaulthome);
			pstmt.setString(idx++, policyOPTDO.defaultshell);
			pstmt.setString(idx++, policyOPTDO.cresult);
			pstmt.setInt(idx++, policyOPTDO.nstatus);
			pstmt.setString(idx++, TimeUtil.getDateString(l_cur_date, "yyyyMMddHHmmss"));

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

    /** 
	 * @author KJH
     * 시스템 기본정책 정보 변경
     *   
     */  
    public void updateSystemPolicyOPT(TSASystemPolicyOPTDO policyOPTDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

			long l_cur_date = TimeUtil.getCurrentTime();

            queryBuf = new StringBuffer();
			queryBuf.append("UPDATE TSA_SYSTEM_POLICYOPT " )
				.append(" SET ")
				.append("  PASSMAXDAY=? ,PASSMINDAY=? ,PASSWARN=? ,INACTIVEDAY=? ,LOGINRETRY=? ")
				.append(" ,RETRYRESET=? ,SUSPENDTIME=? ,UNUSEDTIME=? ,SESSIONCNT=? ,SESSIONTIMEOUT=? ")
				.append(" ,PASSMINLEN=? ,PASSMAXLEN=? ,PASSHISTORY=? ,PASSMINALPHA=? ,PASSMINDIGIT=? ")
				.append(" ,PASSMINUALPHA=? ,PASSMINLALPHA=? ,PASSMINSPECIAL=? ,PASSMINDIFF=? ,PASSMAXSEQ=? ")
				.append(" ,PASSMAXREPEAT=? ,DEFAULTHOME=? ,DEFAULTSHELL=? ,CRESULT=? ,NSTATUS=? ")
				.append(" ,LASTSYNCDATE=? ")
				.append(" WHERE SYSTEMID=? ");

			debug.debug("updateSystemPolicyOPT ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setInt(idx++, policyOPTDO.passmaxday);
			pstmt.setInt(idx++, policyOPTDO.passminday);
			pstmt.setInt(idx++, policyOPTDO.passwarn);
			pstmt.setInt(idx++, policyOPTDO.inactiveday);
			pstmt.setInt(idx++, policyOPTDO.loginretry);
			pstmt.setInt(idx++, policyOPTDO.retryreset);
			pstmt.setInt(idx++, policyOPTDO.suspendtime);
			pstmt.setInt(idx++, policyOPTDO.unusedtime);
			pstmt.setInt(idx++, policyOPTDO.sessioncnt);
			pstmt.setInt(idx++, policyOPTDO.sessiontimeout);
			pstmt.setInt(idx++, policyOPTDO.passminlen);
			pstmt.setInt(idx++, policyOPTDO.passmaxlen);
			pstmt.setInt(idx++, policyOPTDO.passhistory);
			pstmt.setInt(idx++, policyOPTDO.passminalpha);
			pstmt.setInt(idx++, policyOPTDO.passmindigit);
			pstmt.setInt(idx++, policyOPTDO.passminualpha);
			pstmt.setInt(idx++, policyOPTDO.passminlalpha);
			pstmt.setInt(idx++, policyOPTDO.passminspecial);
			pstmt.setInt(idx++, policyOPTDO.passmindiff);
			pstmt.setInt(idx++, policyOPTDO.passmaxseq);
			pstmt.setInt(idx++, policyOPTDO.passmaxrepeat);
			pstmt.setString(idx++, policyOPTDO.defaulthome);
			pstmt.setString(idx++, policyOPTDO.defaultshell);
			pstmt.setString(idx++, policyOPTDO.cresult);
			pstmt.setInt(idx++, policyOPTDO.nstatus);
			pstmt.setString(idx++, TimeUtil.getDateString(l_cur_date, "yyyyMMddHHmmss"));
			pstmt.setString(idx++, policyOPTDO.systemid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

	/**
	 * @author : KJH 
	 * 시스템 기본정책 정보 출력
	 *
	 */
	public TSASystemPolicyOPTDO selectSystemPolicyOPT(String systemid) throws SQLException {

		PreparedStatement pstmt=null;
		ResultSet rs = null;

		TSASystemPolicyOPTDO policyOPTDO = null;

		try {
			
			StringBuffer queryBuf = new StringBuffer();
			queryBuf.append("SELECT ")
				.append("  PASSMAXDAY ,PASSMINDAY ,PASSWARN ,INACTIVEDAY ,LOGINRETRY ")
				.append(" ,RETRYRESET ,SUSPENDTIME ,UNUSEDTIME ,SESSIONCNT ,SESSIONTIMEOUT ")
				.append(" ,PASSMINLEN ,PASSMAXLEN ,PASSHISTORY ,PASSMINALPHA ,PASSMINDIGIT ")
				.append(" ,PASSMINUALPHA ,PASSMINLALPHA ,PASSMINSPECIAL ,PASSMINDIFF ,PASSMAXSEQ ")
				.append(" ,NSTATUS ,PASSMAXREPEAT ,DEFAULTHOME ,DEFAULTSHELL ,CRESULT ")
				.append(" ,LASTSYNCDATE ")
				.append(" FROM TSA_SYSTEM_POLICYOPT ")
				.append(" WHERE SYSTEMID=? ");

			debug.debug("selectSystemPolicyOPT ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, systemid);

			rs = pstmt.executeQuery();

			if ( rs.next() ){
				policyOPTDO = new TSASystemPolicyOPTDO();
				policyOPTDO.systemid = systemid;
				policyOPTDO.passmaxday = rs.getInt("PASSMAXDAY");
				policyOPTDO.passminday = rs.getInt("PASSMINDAY");
				policyOPTDO.passwarn = rs.getInt("PASSWARN");
				policyOPTDO.inactiveday = rs.getInt("INACTIVEDAY");
				policyOPTDO.loginretry = rs.getInt("LOGINRETRY");
				policyOPTDO.retryreset = rs.getInt("RETRYRESET");
				policyOPTDO.suspendtime = rs.getInt("SUSPENDTIME");
				policyOPTDO.unusedtime = rs.getInt("UNUSEDTIME");
				policyOPTDO.sessioncnt = rs.getInt("SESSIONCNT");
				policyOPTDO.sessiontimeout = rs.getInt("SESSIONTIMEOUT");
				policyOPTDO.passminlen = rs.getInt("PASSMINLEN");
				policyOPTDO.passmaxlen = rs.getInt("PASSMAXLEN");
				policyOPTDO.passhistory = rs.getInt("PASSHISTORY");
				policyOPTDO.passminalpha = rs.getInt("PASSMINALPHA");
				policyOPTDO.passmindigit = rs.getInt("PASSMINDIGIT");
				policyOPTDO.passminualpha = rs.getInt("PASSMINUALPHA");
				policyOPTDO.passminlalpha = rs.getInt("PASSMINLALPHA");
				policyOPTDO.passminspecial = rs.getInt("PASSMINSPECIAL");
				policyOPTDO.passmindiff = rs.getInt("PASSMINDIFF");
				policyOPTDO.passmaxseq = rs.getInt("PASSMAXSEQ");
				policyOPTDO.nstatus = rs.getInt("NSTATUS");
				policyOPTDO.passmaxrepeat = rs.getInt("PASSMAXREPEAT");
				policyOPTDO.defaulthome = rs.getString("DEFAULTHOME");
				policyOPTDO.defaultshell = rs.getString("DEFAULTSHELL");
				policyOPTDO.cresult = rs.getString("CRESULT");
				policyOPTDO.lastsyncdate = rs.getString("LASTSYNCDATE");

			}

			rs.close();
			pstmt.close();

		}catch (SQLException e){
			throw e;
		}finally{
			if ( rs != null) try{rs.close();}catch(SQLException ee){}
			if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
		}

		return policyOPTDO;
	}

    /** 
	 * @author KJH
     * 시스템 기본정책 삭제
     *   
     */  
    public void deleteSystemPolicyOPT(String systemid) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

            queryBuf = new StringBuffer();
			queryBuf.append("DELETE FROM TSA_SYSTEM_POLICYOPT " )
				.append(" WHERE SYSTEMID=? ");

			debug.debug("deleteSystemPolicyOPT ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, systemid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

}
